package com.anyonavinfo.cpaddemo.view.mainview.minimusicplayer;


import com.anyonavinfo.cpaddemo.data.musicinfo.MusicInfo;
import com.anyonavinfo.cpaddemo.data.musicinfo.source.MusicInfoDataSource;
import com.anyonavinfo.cpaddemo.data.musicinfo.source.MusicInfoRepository;
<<<<<<< HEAD
import com.anyonavinfo.cpaddemo.service.musicplayerservice.MusicPlayerService;
import com.anyonavinfo.cpaddemo.service.musicplayerservice.MusicPlayerServiceHelper;
import com.anyonavinfo.cpaddemo.service.musicplayerservice.MusicPlayerServiceManager;

import java.util.List;


=======
import com.anyonavinfo.cpaddemo.service.musicplayerservice.MusicServiceManager;

import java.util.List;

>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
/**
 * Created by ZuiC on 2017/1/16.
 */

<<<<<<< HEAD
public class MiniMusicPlayerPresenter implements MiniMusicPlayerContract.Presenter, MusicPlayerServiceHelper.MusicPlayerServiceCallBack {

    private MiniMusicPlayerContract.View mView;
    private MusicInfoRepository mRepository;
    private MusicPlayerService mService;
    private MusicPlayerServiceHelper mServiceHelper;
    private int playMode, playStatus;
    private boolean isShow;


    public MiniMusicPlayerPresenter(MiniMusicPlayerContract.View view, MusicInfoRepository musicInfoRepository){
        this.mView = view;
        mView.setPresenter(MiniMusicPlayerPresenter.this);
        this.mRepository = musicInfoRepository;
        mServiceHelper = MusicPlayerServiceHelper.getInstance();
        MusicPlayerServiceHelper.registerObserver(this);
        isShow = true;
=======
public class MiniMusicPlayerPresenter implements MiniMusicPlayerContract.Presenter {

    private final MusicServiceManager mServiceManager;
    private MiniMusicPlayerContract.View mView;
    private MusicInfoRepository mRepository;
    private List<MusicInfo> mMusicInfoList;

    public MiniMusicPlayerPresenter(MiniMusicPlayerContract.View view, MusicInfoRepository musicInfoRepository){
        this.mView = view;
        this.mRepository = musicInfoRepository;
        mServiceManager= MusicServiceManager.getInstance();
        mView.setPresenter(MiniMusicPlayerPresenter.this);
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
    }

    @Override
    public void start() {
<<<<<<< HEAD
        mRepository.loadMusicInfo(new MusicInfoDataSource.LoadMusicInfoCallBack() {
            @Override
            public void onMusicInfoLoaded(List<MusicInfo> musicInfoList) {
                mRepository.getCurMusicInfo(new MusicInfoDataSource.GetCurMusicInfoCallBack() {
                    @Override
                    public void onGetCurMusicInfo(MusicInfo musicInfo) {
                        mView.updateMusicInfo(musicInfo);
                    }
                });

                mRepository.getCurPlayMode(new MusicInfoDataSource.GetCurPlayModeCallBack() {
                    @Override
                    public void onGetCurPlayMode(int mode) {
                        playMode = mode;
                        mView.updatePlayMode(mode);
                    }
                });

                mRepository.getCurPlayStatus(new MusicInfoDataSource.GetCurPlayStatusCallBack() {
                    @Override
                    public void onGetCurPlayStatus(int status) {
                        playStatus = status;
                        mView.updatePlayStatus(status);
                    }
                });
=======
        loadMusicInfo();
    }

    private void loadMusicInfo(){
        mRepository.loadMusicInfo(new MusicInfoDataSource.LoadMusicInfoCallBack() {
            @Override
            public void onMusicInfoLoaded(List<MusicInfo> musicInfoList) {
                //musicInfoList要取出来用
                mMusicInfoList=musicInfoList;
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
            }
        });
    }

    @Override
<<<<<<< HEAD
    public void playStopCurMusic() {
        if (playStatus == 0){
            mRepository.getCurMusicInfo(new MusicInfoDataSource.GetCurMusicInfoCallBack() {
                @Override
                public void onGetCurMusicInfo(MusicInfo musicInfo) {
                    mService.playMusic(musicInfo);
                }
            });

            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                }
            });
        }else {
            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                    mService.pauseMusic();
                }
            });
        }

    }

    @Override
    public void playNextMusic() {
        if (playStatus == 0){
            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                }
            });
        }
        mRepository.getNextMusicInfo(new MusicInfoDataSource.GetNextMusicInfoCallBack() {
            @Override
            public void onGetNextGetMusicInfo(MusicInfo musicInfo) {
                mService.playMusic(musicInfo);
                mView.updateMusicInfo(musicInfo);
            }
        });
    }

    @Override
    public void playPreviousMusic() {
        if (playStatus == 0){
            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                }
            });
        }
        mRepository.getPreviousMusicInfo(new MusicInfoDataSource.GetPreviousMusicInfoCallBack() {
            @Override
            public void onGetPreviousMusicInfo(MusicInfo musicInfo) {
                mService.playMusic(musicInfo);
                mView.updateMusicInfo(musicInfo);
            }
        });
    }

    @Override
    public void changePlayMode() {
        mRepository.changeCurPlayMode(new MusicInfoDataSource.ChangeCurPlayModeCallBack() {
            @Override
            public void onChangeCurPlayMode(int mode) {
                playMode = mode;
                mView.updatePlayMode(playMode);
            }
        });
    }

    @Override
    public void openMusicPlayerView() {
        isShow = false;
        mView.showMusicPlayerView();
    }


    @Override
    public void refreshView() {
        isShow = true;
        mRepository.getCurMusicInfo(new MusicInfoDataSource.GetCurMusicInfoCallBack() {
            @Override
            public void onGetCurMusicInfo(MusicInfo musicInfo) {
                mView.updateMusicInfo(musicInfo);
            }
        });

        mRepository.getCurPlayMode(new MusicInfoDataSource.GetCurPlayModeCallBack() {
            @Override
            public void onGetCurPlayMode(int mode) {
                playMode = mode;
                mView.updatePlayMode(mode);
            }
        });

        mRepository.getCurPlayStatus(new MusicInfoDataSource.GetCurPlayStatusCallBack() {
            @Override
            public void onGetCurPlayStatus(int status) {
                playStatus = status;
                mView.updatePlayStatus(status);
            }
        });
    }

    @Override
    public void onMusicPlayBackEnding() {
        if (isShow){
            mRepository.getMusicInfoWhenEnding(new MusicInfoDataSource.getMusicInfoWhenEndingCallBack() {
                @Override
                public void onGetMusicInfoWhenEnding(MusicInfo musicInfo) {
                    mService.playMusic(musicInfo);
                    mView.updateMusicInfo(musicInfo);
                }
            });
        }
    }

    @Override
    public void onServiceConnected(MusicPlayerService musicPlayerService) {
        mService = musicPlayerService;
    }

    @Override
    public void updateProgress(int curProgress, int percent) {

=======
    public void play() {
        mServiceManager.play();
    }

    @Override
    public void play(int index) {

    }

    @Override
    public void pause() {
        mServiceManager.pause();
    }

    @Override
    public void previous() {
        mServiceManager.previous();
    }

    @Override
    public void next() {
        mServiceManager.next();
    }

    @Override
    public boolean isPlaying() {
        return mServiceManager.isPlaying();
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
    }
}
